<template>
    <!-- 组件的模板 -->
    <div class="page show-footerBtn">
        <div class="weui-cells">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label title">名称</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="text" placeholder="例如：引导组工资单" maxlength="15" v-model="order_name">
                </div>
            </div>
        </div>
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectStartTime">
                <div class="weui-cell__bd">
                    <p>开始核算日期</p>
                </div>
                <div class="weui-cell__ft">{{start_date}}</div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectEndTime">
                <div class="weui-cell__bd">
                    <p>结束核算日期</p>
                </div>
                <div class="weui-cell__ft">{{end_date}}</div>
            </a>
        </div>
        <div class="weui-cells">            
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoPaySelectApprover">
                <div class="weui-cell__bd">
                    <p>审批人</p>
                </div>
                <div class="weui-cell__ft">{{approve_user_name}}</div>
            </a>
        </div>
        <div class="btn-box bg-color fixed" :style="isIphoneX?'padding-bottom:35px;':''">
            <a href="javascript:;" class="weui-btn weui-btn_primary" @click="gotoPayEmployeeBalance">下一步</a>
        </div>
    </div>
</template>

<script>

    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    // import clientUi from 'gapi/clientUi.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    // import socialApi from 'clientApi/socialApi.js'
    import widget from 'gapi/widget.js'
    import commonPage from 'clientApi/commonPage.js'
    import * as util from 'js/util.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                team_id: 0,
                project_id: 0,
                approve_uid: -1,//审批人id     -1未选择 0无需审批
                approve_user_name: '',//审批人姓名
                order_name: '',//工资单名称
                order_id: 0,//工资单id
                start_date: '',
                end_date: '',
                timestr: '',
                today: '',
                origin_page: '',
                init_data: {
                    start_date: '',
                    end_date: ''
                }
            }
        },
        computed:{
            minDate: function(){
                return util.getLocalTime(this.timestr - 366 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
            },
            isIphoneX: function(){
                return util.isIphoneX()
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.team_id = util.getUrddataString('team_id')
                this.project_id = util.getUrddataString('project_id')
                //今天日期
                let date = new Date()
                this.timestr = date.getTime()
                this.today = util.getLocalTime(this.timestr, 'yyyy-MM-dd')
                
                //判断是否是新建
                this.origin_page = util.getUrddataString('origin_page')
                if(this.origin_page === 'PayListDetail'){//工资单详情
                    // alert('edit')
                    this.order_id = util.getUrddataString('order_id')
                    this.getInitData()
                }else{
                    // alert('new')
                }
            },
            getInitData(){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/wage/order/get',
                    type:'GET',
                    data:{
                        team_id: this.team_id,
                        project_id: this.project_id,
                        order_id: this.order_id
                    },
                    timeout:10000,
                    success:(result) => {
                        console.log(result)
                        commonPage.loadPageStatus('loadSuccess')
                        if(result&&result.errno == 0){
                            this.order_name = result.data.order.order_name
                            this.start_date = this.init_data.start_date = result.data.order.start_date
                            this.end_date = this.init_data.end_date = result.data.order.end_date
                            this.approve_user_name = result.data.order.approve_uid == 0 ? '无需审批' : result.data.order.approve_user_name
                            this.approve_uid = result.data.order.approve_uid
                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            selectStartTime(){
                let start = ''
                if(this.end_date){
                    start = util.getLocalTime(util.getStampFromDate(this.end_date) - 31 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
                    if(start <= this.minDate){
                        start = this.minDate
                    }
                }else{
                    start = this.minDate
                }
                console.log("start:"+start)
                KCApiApp.showDatePicker(start, this.end_date || this.today, this.start_date || this.today, (data) => {
                    // alert(JSON.stringify(data.SelectedDate))
                    this.start_date = data.SelectedDate
                }, '核算时间不能超过一个月')
            },
            selectEndTime(){
                let end = ''
                if(this.start_date){
                    end = util.getLocalTime(util.getStampFromDate(this.start_date) + 31 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
                    if(end >= this.today){
                        end = this.today
                    }
                }else{
                    end = this.today
                }
                console.log('end:'+end)
                KCApiApp.showDatePicker(this.start_date || this.minDate, end, this.end_date || this.today, (data) => {
                    this.end_date = data.SelectedDate
                }, '核算时间不能超过一个月')
                    // widget.toast('请先选择起始日期')
            },
            gotoPaySelectApprover(){
                let encodeStr = encodeURIComponent(`team_id=${this.team_id}&project_id=${this.project_id}`)
                location.href = `dmsaas://PaySelectApprover?urdData=${encodeStr}`
            },
            gotoPayEmployeeBalance(){
                //校验
                if(!this.order_name){
                    widget.toast('请输入工资单名称')
                    return
                }
                if(!this.start_date || !this.end_date){
                    widget.toast('请选择核算日期')
                    return
                }
                if(this.approve_uid == -1){
                    widget.toast('请选择审批人')
                    return
                }
                //埋点
                let statisticsLog_data = {
                    event_id: '工资单-下一步',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'next',
                        project_id: this.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                
                // let ajaxData = {
                //     team_id: this.team_id,
                //     project_id: this.project_id,
                //     order_id: this.order_id,
                //     approve_uid: this.approve_uid,//审批人id
                //     order_name: this.order_name,//工资单名称
                //     start_date: this.start_date,
                //     end_date: this.end_date
                // }
                // console.log(JSON.stringify(ajaxData))

                //下一页
                let flag = 0    //日期是否有修改
                if(this.init_data.start_date != '' && (this.init_data.start_date != this.start_date || this.init_data.end_date != this.end_date)){
                    flag = 1
                }
                //保存点击下一步时的日期
                this.init_data.start_date = this.start_date
                this.init_data.end_date = this.end_date
                // console.log(flag)
                let isShow = util.getUrddataString('isShow')//native间传递的一个标识
                let encodeStr = encodeURIComponent(`team_id=${this.team_id}&project_id=${this.project_id}&order_id=${this.order_id}&order_name=${this.order_name}&start_date=${this.start_date}&end_date=${this.end_date}&approve_uid=${this.approve_uid}&flag=${flag}&isShow=${isShow}`)
                location.href = `dmsaas://PayEmployeeBalance?urdData=${encodeStr}`
                // location.href = `dmsaas://PayEditPerDetail?urdData=${encodeStr}`
            }
        },
        created(){

            this.init();
            // commonPage.updateTitleBar({
            //     backAction: 'goback',    // 点击返回按钮的动作（即回调函数名称）
            //     // leftDisplay: false,            // 是否显示左按钮
            //     // leftText: '',                // 左按钮文案
            //     leftAction: 'goback',    // 点击左按钮的动作（即回调函数名称）
            //     // rightDisplay: true,            // 是否显示右按钮
            //     // rightText: '确认导出',            // 右按钮文案
            //     // rightAction: 'exportSign'    // 点击右按钮的文案（即回调函数名称）
            // })
            // window.goback = this.goback
            window.loadData = (data) => {
                // alert(123)
                console.log(data)
                if(data === undefined)return
                if(data.approve_uid !== undefined){
                    this.approve_uid = data.approve_uid
                    this.approve_user_name = data.approve_user_name
                }
                
            }
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/salary.css"></style>



